package com.aischool.eduorg.api;

import com.aischool.common.model.AiOrg;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiOrgService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/eduorg/org")
public class OrgController extends BaseApiEduorgController {


	@Inject
	AiOrgService aiOrgService;
	
	public void getOrg() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		int login_org_id = this.getAttrForInt("login_org_id");
		
		Object obj = aiOrgService.getOrg(login_org_id);
		if (obj != null) {
			jr.setDicKV("orginfo", obj);
		}
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {

		// 结果类
		JsonResult jr = new JsonResult();
		// 参数
		int login_org_id = this.getAttrForInt("login_org_id");

		// 执行结果
		boolean bf = false;
		
		if (StrKit.isBlank(getPara("org_name",""))) {
			jr.appendMsg("请输入机构名称！");
		}else if (StrKit.isBlank(getPara("link_tel",""))) {
			jr.appendMsg("请输入机构热线！");
		}else if (StrKit.isBlank(getPara("area_names",""))) {
			jr.appendMsg("请选择省市县街道！");
		}else if (StrKit.isBlank(getPara("area_address",""))) {
			jr.appendMsg("请输入详细地址！");
		}else if (StrKit.isBlank(getPara("family_code",""))) {
			jr.appendMsg("请输入家长端邀请码！");
		}else if (StrKit.isBlank(getPara("teacher_code",""))) {
			jr.appendMsg("请输入教师端邀请码！");
		}else {
			
			AiOrg info = getModel(AiOrg.class, "", true);	
			
			Record rdold = aiOrgService.getOrg(info.getFamilyCode(), info.getTeacherCode());
			if (rdold != null && rdold.getInt("org_id") != login_org_id) {
				if (rdold.get("family_code") == info.getFamilyCode()){
					jr.appendMsg("家长端邀请码 "+info.getFamilyCode()+"，已被占用！请重新输入！");
//				}else if (rdold.get("techer_code") == info.getTeacherCode()){
//					jr.appendMsg("教师端邀请码 "+info.getFamilyCode()+"，已被占用！请重新输入！");
				}else {
					jr.appendMsg("邀请码已存在！");
				}
				
			}else {
				info.setOrgId(login_org_id);
				info.setUpdateTime(DateUtil.getTimestamp());
				info.removeNullValueAttrs();
				// 更新
				bf = info.update();
			}
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			String msg = jr.getMsg();
			jr.setEnum(JsonCodeEnum.Faild);
			jr.appendMsg(msg);
		}

		renderJson(jr.Dic);
	}
	
	public void saveDesc() {

		// 结果类
		JsonResult jr = new JsonResult();
		// 参数
		int login_org_id = this.getAttrForInt("login_org_id");

		// 执行结果
		boolean bf = false;
		
		if (StrKit.isBlank(getPara("org_desc",""))) {
			jr.appendMsg("请输入介绍！");
		}else {
			AiOrg info = new AiOrg();	
			
			info.setOrgId(login_org_id);
			info.setOrgDesc(getPara("org_desc",""));
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			// 更新
			bf = info.update();
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			String msg = jr.getMsg();
			jr.setEnum(JsonCodeEnum.Faild);
			jr.appendMsg(msg);
		}

		renderJson(jr.Dic);
	}
}
